package com.qdu.dao.impl;

import com.qdu.dao.ManagementDao;
import com.qdu.entity.Management;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lenovo
 */
public class ManagementDaoImpl extends BaseDaoImpl implements ManagementDao{

    @Override
    public List<Management> selectAll() {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<Management> list=new ArrayList<>();
        
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from Management");
            rs = ps.executeQuery();
            while(rs.next())
            {
                list.add(new Management(rs.getString(1),rs.getString(2)));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public Management selectOneByName(String name) {
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        Management man=null;
        try{
            con=DatabaseUtil.getConnection();
            ps=con.prepareStatement("select * from Management where Mname=?");
            ps.setString(1, name);
            rs = ps.executeQuery();
            while(rs.next())
            {
                man=new Management(rs.getString(1),rs.getString(2));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            DatabaseUtil.close(rs, ps, con);
        }
        return man;
    }

    @Override
    public int updatePwd(String name, String pwd) {
        return executeUpdate("update Management set Mpassword=? where Mname=? ",pwd,name);
    }

    
}
